package cn.apestar.common.image;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class FileUtilsIo {

    public static void deleteFileFromDisk(String filePath) {
        try {
            File file = new File(filePath);
            FileUtils.forceDelete(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据业务ID删掉所有的文档数据
     * @param filePath
     */
    public static void deleteBydataId(String dataId) {
        try {
            // ICcCmFileService ccCmFileService = (ICcCmFileService)Sc.getBean("tlcartel.xtsz.CcCmFileService");
            // ccCmFileService.deleteBydataId(dataId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据业务ID删掉所有的文档数据
     * @param filePath
     */
    public static void deleteBydataId(String dataId, String fileKind) {
        try {
            // ICcCmFileService ccCmFileService = (ICcCmFileService)Sc.getBean("tlcartel.xtsz.CcCmFileService");
            // ccCmFileService.deleteBydataId(dataId,fileKind);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 向指定OutputStream输出文件
     * @param bf
     * @param output
     * @throws IOException
     */
    public static void outputFileByPath(String path, OutputStream output) throws IOException {
        File file = new File(path);
        InputStream input = FileUtils.openInputStream(file);
        try {
            IOUtils.copy(input, output);
        } finally {
            try {
                input.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * 读取文件
     * @param file_id:读取的文件名
     * @return
     * @throws IOException
     */
    public static InputStream readFileFromDisk(String file_path) throws IOException {
        File file = new File(file_path);
        return FileUtils.openInputStream(file);
    }

    /**
     * 读取文件 -路径
     * @param file_id:读取的文件名
     * @return
     * @throws IOException
     */
    public static InputStream readFileFromDiskByPath(String file_path) throws IOException {
        try {
            File file = new File(file_path);
            return FileUtils.openInputStream(file);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 文件流转为字符串
     * @param 字符串
     * @return
     */
    public static String inputStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    /**
     * 文件下载时，response.setContentType（）的值
     * @param filetype
     * @return
     */
    public static String getContentType(String filetype) {
        String in_str = filetype.toLowerCase();
        if (in_str.startsWith(".")) {
            in_str = in_str.substring(1);
        }
        if (in_str.contains(".")) {
            String[] str = in_str.split("\\.");
            in_str = str[1];
        }

        // 文本
        if ("doc".equals(in_str) || "docx".equals(in_str))
            return "application/msword";
        else if ("xls".equals(in_str) || "xlsx".equals(in_str))
            return "application/x-xls";
        else if ("js".equals(in_str))
            return "application/x-javascript";
        else if ("txt".equals(in_str))
            return "text/plain";
        else if ("htm".equals(in_str) || "jsp".equals(in_str))
            return "text/html";
        // 图形
        else if ("bmp".equals(in_str))
            return "application/x-bmp";
        else if ("gif".equals(in_str))
            return "image/gif";
        else if ("png".equals(in_str))
            return "image/png";
        else if ("jpg".equals(in_str) || "jpeg".equals(in_str))
            return "image/jpeg";
        // 视频音频
        else if ("avi".equals(in_str))
            return "video/avi";
        else if ("mid".equals(in_str))
            return "audio/mid";
        else if ("mp3".equals(in_str))
            return "audio/mp3";
        else if ("mp4".equals(in_str))
            return "audio/mpeg4";
        else if ("mpeg".equals(in_str))
            return "audio/mpg";
        else if ("rm".equals(in_str))
            return "application/vnd.rn-realmedia";
        else if ("rmvb".equals(in_str))
            return "application/vnd.rn-realmedia-vbr";
        else if ("wav".equals(in_str))
            return "audio/wav";
        // 其它
        else
            return "application/octet-stream";
    }

    /**
     * 向指定OutputStream输出文件
     * @param bf
     * @param output
     * @throws IOException
     */
    public static void outputFile(String path, OutputStream output) throws IOException {
        if (path != null) {
            InputStream input = FileUtilsIo.readFileFromDisk(path);
            try {
                IOUtils.copy(input, output);
            } finally {
                try {
                    input.close();
                } catch (Exception e) {
                }
            }
        }
    }

    /**
     * 向指定OutputStream输出文件
     * @param bf
     * @param output
     * @throws IOException
     */
    public static void outputFile(File file, OutputStream output) throws IOException {
        if (file != null) {
            InputStream input = FileUtils.openInputStream(file);
            try {
                IOUtils.copy(input, output);
            } finally {
                try {
                    input.close();
                } catch (Exception e) {
                }
            }
        }
    }
}
